package com.newsproject.pojo;

public class Result<T> {
    private Integer code;     //状态码(200,404,500...)
    private String message;   //提示信息
    private T data;           //返回业务数据(数据类型为泛型)

    // 添加简化版success方法
    public static <T> Result<T> success(T data) {
        return success(data,"操作成功");
    }

    //200
    public static <T> Result<T> success(T data,String message){
        Result<T> result = new Result<>();
        result.setCode(200);
        result.setMessage(message);
        result.setData(data);
        return result;
    }

    //404
    public static <T> Result<T> fail(String message){
        Result<T> result = new Result<>();
        result.setCode(404);
        result.setMessage(message != null ? message : "资源未找到");
        return result;
    }

    //500


public static <T> Result<T> error(Integer code,String message){
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setMessage(message);
        return result;
}

public static <T> Result<T> error(String message) {
        return error(500,message);
    }


    // 401 未认证
    public static <T> Result<T> unauthorized(String message) {
        Result<T> result = new Result<>();
        result.setCode(401);
        result.setMessage(message != null ? message : "未认证");
        return result;
    }

    // 403 禁止访问
    public static <T> Result<T> forbidden(T data) {
        Result<T> result = new Result<>();
        result.setCode(403);
        result.setMessage("禁止访问");
        result.setData(data);
        return result;
    }

    // 400 错误请求
    public static <T> Result<T> badRequest(String message) {
        Result<T> result = new Result<>();
        result.setCode(400);
        result.setMessage(message != null ? message : "错误请求");
        return result;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public void setData(T data) {
        this.data = data;
    }

    public Integer getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

    public T getData() {
        return data;
    }


}